package com.aixin.project.db;

import java.util.List;

import com.aixin.project.data.CourseOrder;
import com.aixin.project.data.User;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 12-4-14
 * Time: 上午2:24
 * To change this template use File | Settings | File Templates.
 */
public class UserDao extends Dao {
    public static User find(long uid) {
        return (User) find("select * from user where id=?", User.class, uid);
    }

//    public static long insert(User user) {
//        long id = insert("insert into User(username,password) values(?,?)",user.getUsername(),user.getPassword());
//        if(id>0) user.setId(id);
//        return id;
//    }
    
    public static long queryAllUserscount(){
/*    	String sql = "select * from User where verified=0";
        List<User> allUsers = list(sql,User.class);
        return allUsers.size();*/
        String sql = "select count(*) as c from user where verified=0";
        Object o = findBy(sql, "c",null);
        long count = o==null?0:(Long)o;
        return count;
    }
    
    /*
     * public static List<CourseOrder> listPagenation(int page,int psize,int status){
        int start = (page-1)*psize;
        StringBuffer sql = new StringBuffer("select * from courseorder");
        if(status!=-1)
            sql.append(" where status=").append(status);
        sql.append(" order by id desc limit ?,?");
        System.out.println(sql.toString());
        return list(sql.toString(),CourseOrder.class,start,psize);
    }
     */
    
    public static List<User> listPagenation(int page,int psize,int status){
        int start = (page-1)*psize;
        StringBuffer sql = new StringBuffer("select * from user");
        if(status!=-1)
            sql.append(" where verified=").append(status);
        sql.append(" order by id desc limit ?,?");
        return list(sql.toString(),User.class,start,psize);
    }
    
    public static List<User> queryAllUsers(){
        String sql = "select * from user";
        List<User> allUsers = list(sql,User.class);
        return allUsers;
    }
    
    public static User find(String username,String password){
    	return (User) find("select * from user where username=? and password=? and verified=1", User.class, username,password);
    }
    
    public static long insert(User user){
    	//,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())
    	long id = insert("insert into user(username,password,contactschool,contactperson,contactdetails,contactemail) values(?,?,?,?,?,?)",user.getUsername(),user.getPassword(),user.getContactschool(),user.getContactperson(),user.getContactdetails(),user.getContactemail());
        return id;
    }
    
    public static User find(String username){
    	return (User) find("select * from user where username=?", User.class, username);
    }
    public static int update(User user){
    	return update("update user set username=?,password=?,contactperson=?,contactdetails=?, contactemail=?,contactschool=?", user.getUsername(),user.getPassword(),user.getContactperson(),user.getContactdetails(),user.getContactemail(),user.getContactschool());
    }
    public static int update(String userid,String status){
    	return update("update user set verified=? where id=?",status,userid);
    }
    public static int updatepwd(String userid,String pwd){
    	return update("update user set password=? where id=?",pwd,userid);
    }
    public static long count(int status){
        String sql = "select count(*) c from user";
        if(status!=-1)
            sql += " where verified="+status;
        return  (Long)findBy(sql, "c", null);
    }
}
